﻿/*  CamHelper (See http://camhelper.codeplex.com/ or https://bitbucket.org/AndiKS/camhelper for more Info)
    Copyright (C) 2011 Andreas Wimmer

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Windows.Forms;
using CamHelper.Engine;
using CamHelper.Facade;
using CamHelper.Interpreter;
using Ninject;

namespace CamHelper.ProcessEditor
{
    public partial class ConfigControl : UserControl
    {
        private bool _loaded = false;
        public string TopHomeDirectory { get; private set; }
        public string TopConfigDirectory { get; private set; }
        public IParameterManager Parameter { get; private set; }
        public IConfig Config { get; private set; }

        public event EventHandler ConfigOrFilesChanged;

        protected virtual void OnConfigOrFilesChanged()
        {
            if (_loaded)
            {
                Parameter.Reload();

                var handler = ConfigOrFilesChanged;
                if (handler != null)
                {
                    handler(this, EventArgs.Empty);
                }
            }
        }

        public ConfigControl()
        {
            TopHomeDirectory = String.Empty;
            TopConfigDirectory = String.Empty;

            InitializeComponent();

            var kernel = new StandardKernel(new RealModul());
            Config = kernel.Get<IConfig>();
            var watcher = kernel.Get<ITopSolidWatcher>();

            // Todo: Ask for Version or check running instance...
#if DEBUG
            txbTopConfig.Text = @"P:\Temp\Methoden TEST";
            txbTopHome.Text = @"P:\Temp\Methoden TEST";
#else
            txbTopConfig.Text = watcher.GetTopSolidRegistryEnvironmentValue(611, TopSolidRegistryEnvironmentKeys.TOPCONFIG);
            txbTopHome.Text = watcher.GetTopSolidRegistryEnvironmentValue(611, TopSolidRegistryEnvironmentKeys.TOPHOME);
#endif
            _loaded = true;
            Parameter = kernel.Get<IParameterManager>();
        }

        private void lklTopHome_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            var dlg = new FolderBrowserDialog();
            dlg.Description = @"Path to TopSolid Home Directory, for Example ""C:\Missler\V611""";
            dlg.SelectedPath = txbTopHome.Text;
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                txbTopHome.Text = dlg.SelectedPath;
            }
        }

        private void lklTopConfig_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            var dlg = new FolderBrowserDialog();
            dlg.Description = @"Path to TopSolid Configuration Directory, for Example ""C:\Missler\ConfigV611""";
            dlg.SelectedPath = txbTopConfig.Text;
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                txbTopConfig.Text = dlg.SelectedPath;
            }
        }

        private void txbTopHome_TextChanged(object sender, System.EventArgs e)
        {
            Config.TopHomeDirectory = txbTopHome.Text;
            OnConfigOrFilesChanged();
        }

        private void txbTopConfig_TextChanged(object sender, System.EventArgs e)
        {
            Config.TopConfigDirectory = txbTopConfig.Text;
            OnConfigOrFilesChanged();
        }
    }
}
